package main

import (
	"fmt"
	"github.com/faiface/beep"
	"github.com/faiface/beep/mp3"
	"github.com/faiface/beep/speaker"
	"log"
	"os"
	"time"
)

func Play(source string) {
	done := make(chan bool)
	f, err := os.Open(source)
	if err != nil {
		log.Fatal(err)
	}
	streamer, format, err := mp3.Decode(f)
	if err != nil {
		fmt.Println("sadas")
		log.Fatal(err)
	}
	defer streamer.Close()

	err = speaker.Init(format.SampleRate, format.SampleRate.N(time.Second/10))
	if err != nil {
		fmt.Println("cuol")
		log.Fatal(err)
	}
	ctrl := &beep.Ctrl{Streamer: beep.Seq(streamer, beep.Callback(func() {
		done <- true
	})), Paused: false}
	speaker.Play(ctrl)
	if <-done {
		Play("music.mp3")
	}
}

func main() {
	//
	//f, err := os.Open("./yinxiao10.mp3")
	//if err != nil {
	//	log.Fatal(err)
	//}
	//streamer, format, err := mp3.Decode(f)
	//if err != nil {
	//	fmt.Println("sadas")
	//	log.Fatal(err)
	//}
	//defer streamer.Close()
	//
	//err = speaker.Init(format.SampleRate, format.SampleRate.N(time.Second/10))
	//if err != nil {
	//	fmt.Println("cuol")
	//	log.Fatal(err)
	//}
	//done := make(chan bool)
	//ctrl := &beep.Ctrl{Streamer: beep.Seq(streamer, beep.Callback(func() {
	//	done <- true
	//})), Paused: false}
	//speaker.Play(ctrl)

	//done := make(chan bool)
	go Play("./yinxiao10.mp3")

	fmt.Println("begin")
	//go func() {
	//	for {
	//		if <-done {
	//			//Play(done, "./music.mp3")
	//		}
	//	}
	//
	//}()
	var str string
	_, _ = fmt.Scanf("%s", &str)
	fmt.Println(str)
}
